package top.haichi.webpaper.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.haichi.webpaper.entity.Result;
import top.haichi.webpaper.entity.Teacher;
import top.haichi.webpaper.mapper.TeacherMapper;
import top.haichi.webpaper.service.TeacherService;
import top.haichi.webpaper.utils.FileUtils;
import top.haichi.webpaper.utils.PatternsUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;


@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 修改老师信息
     * @param teacher
     * @return
     */
    public Result setInformation(Teacher teacher) {
        log.info(teacher+"设置了老师信息");
        return null;
    }

    /**
     * 新增老师信息
     * @param teacher
     * @return
     */
    public Result addTeacher(Teacher teacher) {
        log.info(teacher+"创建了老师信息");

        if(teacher.getId()==null||teacher.getId().length()<1||teacher.getId().length()>16){
            return Result.error("工号必须为1~16位");
        }
        if(teacher.getName()==null||teacher.getName().length()<1||teacher.getName().length()>10){
            return Result.error("姓名必须为1~10位");
        }
        if(teacher.getClassName()==null||teacher.getClassName().length()<1||teacher.getClassName().length()>10){
            return Result.error("班级必须为1~10位");
        }
        if(!(teacher.getGender().equals("男")||teacher.getGender().equals("女")||teacher.getGender().equals("未知"))){
            return Result.error("性别输入不正确");
        }
        if((teacher.getEmail().length()<1||teacher.getEmail().length()>128)){
            return Result.error("邮箱必须为1~128位");
        }
        if(!(PatternsUtils.match(teacher.getEmail(),PatternsUtils.EMAIL_PATTERN))){
            return Result.error("不符合邮箱格式");
        }
        if(teacherMapper.getTeacherByid(teacher)!=null){
            return Result.error("您已经创建过个人信息了，请勿重复创建");
        }
        teacherMapper.addTeacher(teacher);

        return Result.success("信息录入成功");
    }

    /**
     * 查询老师信息
     * @param teacher
     * @return
     */
    public Result getTeacher(Teacher teacher){
        Teacher resultTeacher = teacherMapper.getTeacherByid(teacher);
        return Result.success("查询成功",resultTeacher);
    }

    /**
     * 获取老师头像
     * @param teacher
     * @param response
     * @return
     */
    @Override
    public byte[] getPortrait(Teacher teacher,HttpServletResponse response) {
//        try {
//            File file = new File(FileUtils.getUploadPath() + "\\resources\\" + teacher.getId() + "\\images\\portrait" + ".jpg");
//            FileInputStream fileInputStream = new FileInputStream(file);
//            byte[] data = IOUtils.toByteArray(fileInputStream);
//            fileInputStream.close();
//            return data;
//        }catch (Exception e){
//            e.printStackTrace();
            return null;
//        }
    }

    @Override
    public Result updateTeacher(Teacher teacher) {
        teacherMapper.updateTeacherInformation(teacher);
        return Result.success("修改成功!");
    }
}
